
import re

from django.conf import settings
from django.contrib.auth.backends import ModelBackend
from apps.users.models import User
from meiduo_mall.settings.dev import logger
from utils.secret import SecretOauth


def generate_verify_email_url(user):
    """
    :param user: 用户对象
    :return:
    """
    # 1.加密的数据
    data_dict = {'user_id': user.id, "email": user.email}

    # 2. 进行加密数据
    from utils.secret import SecretOauth
    secret_data = SecretOauth().dumps(data_dict)

    # 3. 返回拼接url
    active_url = settings.EMAIL_ACTIVE_URL + '?token=' + secret_data
    return active_url




# 封装 校验多用户名的方法
def get_user_by_account(account):
    try:
        # 3.1 如果是手机号  验证码手机号
        if re.match('^1[345789]\d{9}$',account):
            user = User.objects.get(mobile=account)
        else:
            # 3.2 不是手机号 验证 username
            user = User.objects.get(username=account)
    except Exception as e:
        logger.error(e)
        return None
    else:
        return user

# 2. 自定义认证后端类
class UsernameMobileAuthBackend(ModelBackend):
    # 3. 重写父类的认证方法
    def authenticate(self, request, username=None, password=None, **kwargs):
        # 1.获取校验完毕的 user
        user = get_user_by_account(username)
        # 2. 校验密码 ,如果通过返回user对象
        if user and user.check_password(password):
            return user
